package com.hunttown.mes.service;

import com.hunttown.mes.common.utils.FastJsonUtils;
import com.hunttown.mes.common.utils.GeneralUtils;
import com.hunttown.mes.common.utils.ScriptFilterUtils;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.dao.TransportSmsTemplateDao;
import com.hunttown.mes.domain.TransportSmsTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * created by wangjunfu on 2021-02-25 10:59:10
 */
@Service
public class TransportSmsTemplateService {

    private final static Logger logger = LoggerFactory.getLogger(TransportSmsTemplateService.class);

    private final TransportSmsTemplateDao transportSmsTemplateDao;
    private final AHelpService helpService;

    @Autowired
    public TransportSmsTemplateService(TransportSmsTemplateDao transportSmsTemplateDao, AHelpService helpService) {
        this.transportSmsTemplateDao = transportSmsTemplateDao;
        this.helpService = helpService;
    }

    /**
     * 新增
     *
     * @param transportSmsTemplate 实体对象
     * @return 最新插入id
     */
    public TransportSmsTemplate insert(TransportSmsTemplate transportSmsTemplate) {
        //api接口传进来是有值的
        if (transportSmsTemplate.getEntId() == null)
            transportSmsTemplate.setEntId(helpService.getEnterpriseId());
        if (transportSmsTemplate.getAdminId() == null)
            transportSmsTemplate.setAdminId(0);
        if (transportSmsTemplate.getCreateTime() == null)
            transportSmsTemplate.setCreateTime(new Date());
        if (transportSmsTemplate.getStopFlag() == null)
            transportSmsTemplate.setStopFlag(0);
        if (transportSmsTemplate.getDeleteFlag() == null)
            transportSmsTemplate.setDeleteFlag(0);

        if (ScriptFilterUtils.xssFilterForDomain(GeneralUtils.ergodicModel(FastJsonUtils.toJSONString(transportSmsTemplate), TransportSmsTemplate.class))) {
            return transportSmsTemplate;
        }
        return transportSmsTemplateDao.insert(transportSmsTemplate);
    }

    /**
     * 根据ID修改数据信息
     *
     * @param transportSmsTemplate 实体对象
     * @return True or False
     */
    public Boolean updateInfoById(TransportSmsTemplate transportSmsTemplate) {
        if (transportSmsTemplate.getEntId() == null) {
            transportSmsTemplate.setEntId(helpService.getEnterpriseId());
        }
        if (ScriptFilterUtils.xssFilterForDomain(GeneralUtils.ergodicModel(FastJsonUtils.toJSONString(transportSmsTemplate), TransportSmsTemplate.class))) {
            return false;
        }
        return transportSmsTemplateDao.updateInfoById(transportSmsTemplate);
    }

    /**
     * 根据Query修改数据信息
     *
     * @param query 需要修改的字段 KV 格式
     * @return True or False
     */
    public Boolean updateInfoByQuery(Query query) {
        if (!query.containsKey("w_entId")) {
            query.put("w_entId", helpService.getEnterpriseId());
        }

        if (ScriptFilterUtils.xssFilterForQuery(query)) {
            return false;
        }
        return transportSmsTemplateDao.updateInfoByQuery(query);
    }

    /**
     * 根据ID获取记录信息
     *
     * @param id 主键id
     * @return 实体对象
     */
    public TransportSmsTemplate selectById(Integer id) {
        return transportSmsTemplateDao.selectById(id, helpService.getEnterpriseId());
    }

    /**
     * 根据query取出一条记录，但是可能有多条记录，取最新的一条。
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象
     */
    public TransportSmsTemplate selectObjByQuery(Query query) {
        if (query == null || query.size() == 0) {
            return null;
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        List<TransportSmsTemplate> list = selectByQuery(query);
        if (list == null || list.size() == 0) {
            return null;
        }

        return list.get(0);
    }

    /**
     * 根据条件获取List
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象列表
     */
    public List<TransportSmsTemplate> selectByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return transportSmsTemplateDao.selectByQuery(query);
    }

    /**
     * 根据条件获取记录总数
     *
     * @param query 查询条件 KV 格式
     * @return 总记录数
     */
    public Long countByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return transportSmsTemplateDao.countByQuery(query);
    }

    /**
     * 根据ID删除记录信息
     *
     * @param id 主键id
     * @return True or False
     */
    public Boolean deleteById(Integer id) {
        return transportSmsTemplateDao.deleteById(id, helpService.getEnterpriseId());
    }

    // 通过唯一识别号获取
    private TransportSmsTemplate getByCode(String code, Integer entId) {
        if (StringUtils.isBlank(code)) {
            return null;
        }

        Query query = Query.noPage();
        query.put("entId", entId);
        query.put("discernCode", code.trim());
        query.put("stopFlag", 0);
        return selectObjByQuery(query);
    }

    // 通过短信类型获取获取最新的一条配置（所以，库中每个类型的配置应该是唯一的）
    private TransportSmsTemplate getBySmsType(Integer smsType, Integer templateType, Integer entId) {
        if (smsType == null || smsType < 0) {
            return null;
        }

        Query query = Query.noPage();
        query.put("entId", entId);
        query.put("smsType", smsType);
        query.put("templateType", templateType);
        query.put("stopFlag", 0);
        query.put("orderBy", "order by id desc");
        return selectObjByQuery(query);
    }

    // 通过短信类型获取获取【模板ID】
    public String getTempleteIdBySmsType(Integer smsType, Integer templateType, Integer entId) {
        TransportSmsTemplate dto = getBySmsType(smsType, templateType, entId);
        return dto == null ? "" : dto.getDiscernCode();
    }

    // 通过短信类型获取获取【模板内容】
    String getTempleteContentBySmsType(Integer smsType, Integer templateType, Integer entId) {
        TransportSmsTemplate dto = getBySmsType(smsType, templateType, entId);
        return dto == null ? "" : dto.getSmsTemplate();
    }

    // 通过短信类型获取获取【模板签名】
    public String getTempleteSignBySmsType(Integer smsType, Integer templateType, Integer entId) {
        TransportSmsTemplate dto = getBySmsType(smsType, templateType, entId);
        return dto == null ? "" : dto.getSmsSign();
    }

    // 通过模板ID获取获取【模板签名】
    String getTempleteSignByID(String templateId, Integer entId) {
        TransportSmsTemplate dto = getByCode(templateId, entId);
        return dto == null ? "" : dto.getSmsSign();
    }

    // 通过主题获取一条配置
    private TransportSmsTemplate getTempleteBySmsTopic(String smsTopic, Integer templateType, Integer entId) {
        if (StringUtils.isBlank(smsTopic)) {
            return null;
        }

        Query query = Query.noPage();
        query.put("entId", entId);
        query.put("smsTopic", smsTopic);
        query.put("templateType", templateType);
        query.put("stopFlag", 0);
        query.put("orderBy", "order by id desc");
        return selectObjByQuery(query);
    }

    // 通过主题获取【模板ID】
    public String getTempleteIdBySmsTopic(String smsTopic, Integer templateType, Integer entId) {
        TransportSmsTemplate dto = getTempleteBySmsTopic(smsTopic, templateType, entId);
        return dto == null ? "" : dto.getDiscernCode();
    }

}